<?php
include '../source/define.php';
$defincluded = true;
include ROOT_PATH .'source/mysql_connect.php';
if(isset($_GET['action']) && $_GET['action'] == "exit"){
    setcookie("logged_in", "", time() - 24*3600, "/");
    header('location: ' . BASE_URL . 'admin/login.php');
}else {
    if (isset($_POST['login']) && isset($_POST['password'])) {
        $userid = mysql_value("SELECT `id` FROM `user` WHERE `login` = ? AND `password` = ?", addslashes(trim($_POST['login'])), md5(addslashes(trim($_POST['password']))));
        if ($userid > 0) {
            setcookie("logged_in", $userid, time() + 24*3600, "/");
             header('location: ' . BASE_URL . 'admin/index.php');
        }
    }
}

if (isset($_COOKIE['logged_in'])) {
    header('location: ' . BASE_URL . 'admin/index.php');
}
include "templates/header.php";
?>
<script>
    $(function(){
        document.getElementById("login").focus();
    });
</script>
<form action="" method="post">
    <div class="login_form">
        <h1>Логин:</h1>
        <div style="width: 100%; height: 24px;"><input id="login" type="text" style="width: 90%; height: 100%;" name="login" /></div>
        <h1>Пароль:</h1>
        <div style="width: 100%; height: 24px;"><input type="password" style="width: 90%; height: 100%;" name="password" /></div>
        <div style="width: 100%; height: 24px; margin-top: 10px;"><input type="submit" style="height: 100%;" value="Войти" /></div>
        <div style="width: 100%; height: 24px; margin-top: 10px;"><a href="<?=BASE_URL;?>">Вернуться</a></div>
    </div>
</form>
<? include ROOT_PATH ."templates/footer.php"; ?>